package utils;

import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class FileUtils {

	/**
	 * Gets the path to a file
	 * All the files should be into "src" folder
	 * The file should be named as if the src folder where the main folder  "/"
	 * Example: Path myPath=FileUtils.getPath("/files/config/Departaments.exp")
	 *          where:  
	 *          the file is "route_to_src_folder/src/files/conf/Departaments.exp"
	 * @param fileNameFromSrcFolder: The relative route to the file from "src" folder
	 * @return path
	 * @throws URISyntaxException 
	 */
	public static Path getPath(String fileNameFromSrcFolder) throws URISyntaxException{
		return Paths.get(FileUtils.class.getResource(fileNameFromSrcFolder).toURI());
	}
	
	/**
	 * Returns the content of a small file into an array of bites 
	 * The file should be named as if the src folder where the main folder  "/"
	 * Example: byte[] aByte=FileUtils.smallFileContent("/files/config/Departaments.exp")
	 *          where: 
	 *          the file is "route_to_src_folder/src/files/conf/Departaments.exp"
	 * @param fileNameFromSrcFolder: The relative route to the file from "src" folder
	 * @return The content of the file into an array of bytes
	 * @throws IOException
	 * @throws URISyntaxException
	 */
	public static byte[] smallFileContent(String fileNameFromSrcFolder) throws IOException, URISyntaxException{
		Path myPath=getPath(fileNameFromSrcFolder);
		return Files.readAllBytes(myPath);
	}
	
	/**
	 * Returns the content of a small file into a string
	 * The file should be named as if the src folder where the main folder  "/"
	 * Example:String contentStr=FileUtils.smallFileContentToString(anyObj,"/files/config/Departaments.exp")
	 *          where: 
	 *          the file is "route_to_src_folder/src/files/conf/Departaments.exp"
	 * @param fileNameFromSrcFolder: The relative route to the file from "src" folder
	 * @return The content of the file into an array of bytes
	 * @throws IOException
	 * @throws URISyntaxException
	 */
	
	public static String readSmallFileContentToString(String fileNameFromSrcFolder) throws IOException, URISyntaxException{
		Path myPath=getPath(fileNameFromSrcFolder);
		return new String(Files.readAllBytes(myPath));
	}
	
	/**
	 * Returns the content of a long file into a list of strings
	 * The file should be named as if the src folder where the main folder  "/"
	 * Example:String contentStr=FileUtils.smallFileContentToString("/files/config/Departaments.exp")
	 *          where: 
	 *          the file is "route_to_src_folder/src/files/conf/Departaments.exp"
	 * @param fileNameFromSrcFolder: The relative route to the file from "src" folder
	 * @return The content of the file into list of string
	 *
	 * @throws URISyntaxException
	 * @throws IOException
	 */
	public static List<String> readLargerTextFileAsListString(String fileNameFromSrcFolder) throws URISyntaxException, IOException {
		List<String> lines=new ArrayList<String>();
		Path myPath=getPath(fileNameFromSrcFolder);
	    //try (Scanner scanner =  new Scanner(path, ENCODING.name())){
		Scanner scanner =  new Scanner(myPath);
		while (scanner.hasNextLine()) lines.add(scanner.nextLine());
		scanner.close();
		return lines;
	}
	
	
	public static void stringAppendToSmallFileContent(String fileNameFromSrcFolder, String content) throws URISyntaxException, IOException{
		Path myPath=getPath(fileNameFromSrcFolder);
		Files.write(myPath, content.getBytes(), StandardOpenOption.APPEND);
	}
	
	public static void stringWriteToSmallFileContent(String fileNameFromSrcFolder, String content) throws URISyntaxException, IOException{
		Path myPath=getPath(fileNameFromSrcFolder);
		Files.write(myPath, content.getBytes(), StandardOpenOption.WRITE);
	}
	
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			System.out.println(getPath("/META-INF/persistence.xml").toString());
			System.out.println("1.- *********************");
			System.out.println(readSmallFileContentToString("/META-INF/persistence.xml"));
			System.out.println("2.- *********************");
			for (String s:readLargerTextFileAsListString("/META-INF/persistence.xml")) 
			  System.out.println(s);
			System.out.println("3.- *********************");
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}
